<template>
  <div>
    <el-card>
      <template #header>
        <div style="text-align:right">
          <el-select v-model="year" @change="changeEvent">
            <el-option
              v-for="item in yearList"
              :key="item"
              :value="item"
              :label="item"
            ></el-option>
          </el-select>
          <el-select v-model="month" @change="changeEvent">
            <el-option
              v-for="item in monthList"
              :key="item"
              :value="item"
              :label="item"
            ></el-option
          ></el-select>
        </div>
      </template>
      <el-calendar v-model="value">
        <template #dateCell="{date,data}">
          <div @click="cellClick(date)" style="width:100%;height:100%">
            <span>{{ data.day.split('-')[2] }}</span>
            <span v-if="data.type === 'current-month'">{{
              date | formatDay
            }}</span>
          </div>
        </template>
      </el-calendar>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'HmCalendar',
  props: {
    valueTime: {
      type: Date,
      default: () => Date.now()
    }
  },
  model: {
    prop: 'valueTime'
  },
  data () {
    return {
      value: new Date(),
      year: '',
      month: '',
      yearList: [],
      monthList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    }
  },
  watch: {
    valueTime: {
      deep: true,
      immediate: true,
      handler (newVal) {
        this.year = newVal.getFullYear()
        this.month = newVal.getMonth() + 1
        const min = this.year - 5
        const max = this.year + 5
        const arr = []
        for (let i = min; i < max; i++) {
          arr.push(i)
        }
        this.yearList = arr
      }
    }
  },
  methods: {
    changeEvent () {
      this.$emit('input', new Date(this.year + '-' + this.month + '-01'))
    },
    cellClick (date) {
      this.$emit('input', date)
    }
  },
  filters: {
    formatDay (time) {
      if (time.getDay() === 0 || time.getDay() === 6) {
        return '休'
      } else {
        return ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-calendar__header {
  display: none;
}
</style>
